//
//  ExpertCenterViewController.m
//  DaJiang
//
//  Created by sally on 16/10/6.
//  Copyright © 2016年 jiniu. All rights reserved.
//

#import "CellManager.h"
#import "ExpertCenterViewController.h"
#import "ExpertModel.h"
#import "ThemeManagerViewController.h"
#import "WithdrawFundsViewController.h"
#import "UserDetailViewController.h"

#define MeetLabelWidth 35   //“可约”文本宽
#define NoMeetLabelWidth 50 //“不可约”文本宽
#define SelectBtnWidth 20   //选择按钮宽

@interface ExpertCenterViewController ()

@property (weak, nonatomic) IBOutlet UIView *headerView;
@property (weak, nonatomic) IBOutlet UIImageView *headImage;
@property (weak, nonatomic) IBOutlet UILabel *nameLabel;
@property (weak, nonatomic) IBOutlet UILabel *jobLabel;
@property (weak, nonatomic) IBOutlet UILabel *locLabel;
@property (weak, nonatomic) IBOutlet UILabel *seenLabel;
@property (weak, nonatomic) IBOutlet UILabel *missLabel;
@property (weak, nonatomic) IBOutlet UITableView *tableView;
@property (strong, nonatomic) NSArray *tableData;
@property (strong, nonatomic) UIButton *selectBtn;      //“可约”的选择按钮
@property (assign, nonatomic) CGFloat contentViewWidth; //可约+按钮宽

@end

@implementation ExpertCenterViewController

- (void)viewDidLoad {
  [super viewDidLoad];
  [self initControl];
  [self initData];
}

#pragma mark 初始化控件
- (void)initControl {
  self.title = @"专家中心";
  self.headerView.height = GExpertImageHeight;
  [self.tableView registerClass:[UITableViewCell class] forCellReuseIdentifier:@"Cell"];
  [[CommonMethod sharedCommonMethod] setExtraCellLineHidden:self.tableView];
  
  self.selectBtn = [[UIButton alloc] init];
  
  //装配头图数据
  [self loadHeadView];
}

#pragma mark 初始化数据
- (void)initData {
  self.tableData = @[ @"约见开放", @"余额提现", @"修改专家资料", @"管理话题", @"查看主页" ];
  self.contentViewWidth = (MeetLabelWidth + SelectBtnWidth);
  
  [UserModel quaryUserInfoWithCompletion:^(UserModel *user) {
    if (user) {
      [[CacheManager shareInstance]cacheLastLoginAccount:user];
      //重新装配头图数据
      [self loadHeadView];
    } else {
      NSError *error = [UserModel getLastError];
      [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
    }
  }];
}

#pragma mark - TableView data source
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView {
  return 1;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
  return self.tableData.count;
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
  UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"Cell"];
  
  //主标题
  [CellManager configCellText:cell type:TextColorType_Black];
  cell.textLabel.text = self.tableData[indexPath.row];
  
  if (indexPath.row == 0) {
    self.selectBtn.frame = CGRectMake(0, 0, SelectBtnWidth, NavHeight);
    self.selectBtn.userInteractionEnabled = NO;
    [self.selectBtn setImage:[UIImage imageNamed:@"ic_selected"] forState:UIControlStateSelected];
    [self.selectBtn setImage:[UIImage imageNamed:@"ic_unselected"] forState:UIControlStateNormal];
    
    UILabel *label = [[UILabel alloc] init];
    if (self.selectBtn.isSelected) {
      label.frame = CGRectMake(SelectBtnWidth, 0, MeetLabelWidth, NavHeight);
      label.text = @"可约";
      label.textColor = GOrangeColor;
      self.contentViewWidth = (MeetLabelWidth + SelectBtnWidth);
    } else {
      label.frame = CGRectMake(SelectBtnWidth, 0, NoMeetLabelWidth, NavHeight);
      label.text = @"不可约";
      label.textColor = GGrayWordColor1;
      self.contentViewWidth = (NoMeetLabelWidth + SelectBtnWidth);
    }
    label.textAlignment = NSTextAlignmentRight;
    label.font = GCustomNormalFont;
    
    UIView *contentView = [[UIView alloc] initWithFrame:CGRectMake(ScreenWidth - self.contentViewWidth - GNormalMargin, 0, self.contentViewWidth, NavHeight)];
    [contentView addSubview:self.selectBtn];
    [contentView addSubview:label];
    [cell.contentView addSubview:contentView];
  } else {
    //右箭头
    cell.accessoryType = UITableViewCellAccessoryDisclosureIndicator;
  }
  
  return cell;
}

#pragma mark TableView delegate
- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath {
  [tableView deselectRowAtIndexPath:indexPath animated:YES]; //返回后选中状态消失
  
  if (indexPath.row == 0) {
    [ExpertModel setMeetAvailableWithAvailable:!self.selectBtn.isSelected completion:^(BOOL isSucceed) {
      if (isSucceed) {
        self.selectBtn.selected = !self.selectBtn.isSelected;
        [self.tableView reloadData];
      } else {
        NSError *error = [ExpertModel getLastError];
        [[CommonMethod sharedCommonMethod] showAlertMessage:error.domain];
      }
    }];
  } else if (indexPath.row == 1) { //余额提现
    WithdrawFundsViewController *vc = [[WithdrawFundsViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
  } else if (indexPath.row == 2) { //修改专家资料
    UserDetailViewController *vc = [[UserDetailViewController alloc] init];
    vc.userType = UserDetailType_Expert;
    vc.userModel = [[CacheManager shareInstance] getLastLoginAccount];
    [self.navigationController pushViewController:vc animated:YES];
  } else if (indexPath.row == 3) { //管理话题
    ThemeManagerViewController *vc = [[ThemeManagerViewController alloc] init];
    [self.navigationController pushViewController:vc animated:YES];
  } else if (indexPath.row == 4) { //查看主页
    UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
    [OperationManager gotoExpertDetailVcWithId:userModel.uid expertName:userModel.user_data.user_name];
  }
}

#pragma mark - 私有方法
#pragma mark 装配头图数据
- (void)loadHeadView {
  UserModel *userModel = [[CacheManager shareInstance]getLastLoginAccount];
  if (userModel) {
    UserData *user = userModel.user_data;
    [self.headImage sd_setImageWithURL:[NSURL URLWithString:user.user_profile_bg_pic] placeholderImage:[UIImage imageNamed:@"large_image_default"]];
    self.nameLabel.text = user.user_name;
    self.jobLabel.text = user.user_pro_title;
    self.locLabel.text = user.user_location_desc;
    self.seenLabel.text = [NSString stringWithFormat:@"%ld人见过", (long) user.pro_user_consulting_count];
    self.missLabel.text = [NSString stringWithFormat:@"%ld人想约", (long) user.like_count];
    self.selectBtn.selected = user.pro_consulting_available;
    [self.tableView reloadData];
  }
}

@end
